package br.com.lopes.gci.util;

import static javax.faces.application.FacesMessage.SEVERITY_ERROR;
import static javax.faces.application.FacesMessage.SEVERITY_INFO;

import java.util.Locale;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;

public class MessageUtil {
	private static MessageUtil instance;
	private ResourceBundle resourceBundleDefault;
	public static final Locale PT_BR = new Locale("pt", "BR");
	public static final Locale EN_US = Locale.US;
	
	private MessageUtil() {
		loadBundleDefault();
	}

	private void loadBundleDefault() {
		resourceBundleDefault = ResourceBundle.getBundle(FacesContext.getCurrentInstance().getApplication().getMessageBundle(), PT_BR);
	}
	
	public static MessageUtil getInstance() {
		if (instance == null) {
			instance = new MessageUtil();
		}
		return instance;
	}

	public String getDefaultMessageFromBundle(String key) {
		return resourceBundleDefault.getString(key);
	}

	public FacesMessage getErrorMessage(final String summary) {
		return createMessage(null, summary, SEVERITY_ERROR);
	}

	public FacesMessage getErrorMessage(final String detail, final String summary) {
		return createMessage(detail, summary, SEVERITY_ERROR);
	}

	public FacesMessage getInfoMessage(final String summary) {
		return createMessage(null, summary, SEVERITY_INFO);
	}

	public FacesMessage getInfoMessage(final String detail, final String summary) {
		return createMessage(detail, summary, SEVERITY_INFO);
	}

	public void setErrorMessage(final String summary) {
		addMessage(getErrorMessage(summary));
	}

	public void setErrorMessage(final String detail, final String summary) {
		addMessage(getErrorMessage(detail, summary));
	}

		public void setInfoMessage(final String summary) {
		addMessage(getInfoMessage(summary));
	}

	public void setInfoMessage(final String detail, final String summary) {
		addMessage(getInfoMessage(detail, summary));
	}

	public void setDefaultMessageFromBundle(final String key) {
		addMessage(getInfoMessage(getDefaultMessageFromBundle(key)));
	}

	public void setErrorMessageForComponent(final String summary, final String clientId) {
		addMessage(getErrorMessage(summary), clientId);
	}

	public void setErrorMessageForComponent(final String detail, final String summary, final String clientId) {
		addMessage(getErrorMessage(detail, summary), clientId);
	}

	public void setInfoMessageForComponent(final String summary, final String clientId) {
		addMessage(getInfoMessage(summary), clientId);
	}

	public void setInfoMessageForComponent(final String detail, final String summary, final String clientId) {
		addMessage(getInfoMessage(detail, summary), clientId);
	}
	
	public boolean isMessageAvaliable() {
		return FacesContext.getCurrentInstance().getMessages().hasNext();
	}

	public boolean isMessageAvaliable(final String clientId) {
		return FacesContext.getCurrentInstance().getMessages(clientId).hasNext();
	}

	private void addMessage(final FacesMessage message) {
		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	private void addMessage(final FacesMessage message, final String clientId) {
		FacesContext.getCurrentInstance().addMessage(clientId, message);
	}

	private FacesMessage createMessage(final String detail, final String summary, final Severity severity) {
		FacesMessage facesMessage = new FacesMessage();
		facesMessage.setDetail(detail);
		facesMessage.setSeverity(severity);
		facesMessage.setSummary(summary);
		return facesMessage;
	}
}